﻿using System.Linq;
using www.Models;

namespace Data.DAO
{
    public class MenuValueDAO//:BaseDAO<RELAXEntities,RelaxMenu>
   {
       #region value
       private RelaxDataContext _context = new RelaxDataContext();
       MenuRefDAO _menuRefDao = new MenuRefDAO();
       #endregion
       #region hàm lấy dữ liệu


       #endregion

       #region Add
       public void Add(MENU_VALUE menu)
       {
           _context.MENU_VALUEs.InsertOnSubmit(menu);
           _context.SubmitChanges();

       }
       #endregion

       #region hàm Update

       public bool Update(MENU_VALUE menuRef)
       {
           var obj = _context.MENU_VALUEs.Where(m => m.menuid == menuRef.menuid && m.languagecode == menuRef.languagecode);
           if (obj.Any())
           {
               var firstOrdefault = obj.FirstOrDefault();
               if (firstOrdefault != null)
               {
                   firstOrdefault.name = menuRef.name;
                   firstOrdefault.tag = menuRef.tag;
                   firstOrdefault.keyword = menuRef.keyword;
                   firstOrdefault.decription = menuRef.decription;
                   _context.SubmitChanges();
                   return true;
               }
           }
           return false;
       }
       #endregion

       #region hàm Delete

       public void Delete(long iId, long iLanguageCode)
       {
           var obj = _context.MENU_VALUEs.Where(m => m.menuid == iId);
           var deleteRef = obj.Count() == 1 || iLanguageCode == 0;
           if (iLanguageCode != 0)
               obj = obj.Where(p => p.languagecode == iLanguageCode);
           foreach (var item in obj)
           {
               _context.MENU_VALUEs.DeleteOnSubmit(item);
           }
           _context.SubmitChanges();
           if (deleteRef)
               _menuRefDao.Delete(iId);
       }
       #endregion

   }
}
